package com.hcy.controller.driver;

import com.hcy.driver.DriverBaseInfo;
import com.hcy.driver.dto.request.BaseInfoRequestDTO;
import com.hcy.driver.dto.request.DriverCarInfoRequestDTO;
import com.hcy.driver.dto.response.DriverCarInfoResponseDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/12/29 18:16
 */
@RestController
@RequestMapping("/driver/car")
public class CarInfoController {

    static final Logger logger = LoggerFactory.getLogger(CarInfoController.class);

    @Autowired
    DriverBaseInfo driverBaseInfo;

    /**
     * @Author huchenying
     * @Description 司机出车
     * @Date
     **/
    @PostMapping("/driverStart")
    public ResponseData<Object> driverStart(@Validated @RequestBody BaseInfoRequestDTO baseInfoRequestDTO) {
        logger.info("driverStart start param -> {}", ToStringBuilder.reflectionToString(baseInfoRequestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            baseInfoRequestDTO.setUseStatus(1);
            boolean res = driverBaseInfo.driverUpdate(baseInfoRequestDTO);
            if (res) {
                responseData.setCode(Code.Success);
            } else {
                responseData.setCode(Code.Fail);
            }
            return responseData;
        } catch (Exception e) {
            logger.error("出车异常", e);
            responseData.setCode(Code.Exception);
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 司机收车车
     * @Date
     **/
    @PostMapping("/driverEnd")
    public ResponseData<Object> driverEnd(@Validated @RequestBody BaseInfoRequestDTO baseInfoRequestDTO) {
        logger.info("driverEnd start param -> {}", ToStringBuilder.reflectionToString(baseInfoRequestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            baseInfoRequestDTO.setUseStatus(0);
            boolean res = driverBaseInfo.driverUpdate(baseInfoRequestDTO);
            if (res) {
                responseData.setCode(Code.Success);
            } else {
                responseData.setCode(Code.Fail);
            }
            return responseData;
        } catch (Exception e) {
            logger.error("收车异常", e);
            responseData.setCode(Code.Exception);
            return responseData;
        }
    }


    /**
     * @Author huchenying
     * @Description 通过driverId获取信息
     * @Date 2022/3/21 17:04
     **/
    @PostMapping("/driverCarInfo")
    public ResponseData<DriverCarInfoResponseDTO> driverCarInfo(@Validated @RequestBody DriverCarInfoRequestDTO requestDTO) {
        logger.info("driverCarInfo start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<DriverCarInfoResponseDTO> responseData = new ResponseData<>();
        try {
            DriverCarInfoResponseDTO responseDTO = driverBaseInfo.getDriverCarInfo(requestDTO.getDriverId());
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            responseData.setCode(Code.Fail);
            responseData.setMsg("请求失败！");
        }
        logger.info("driverCarInfo end result -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 通过carId获取司机和车辆相关信息
     * @Date 2022/3/21 17:04
     **/
    @PostMapping("/carDriverInfo")
    public ResponseData<DriverCarInfoResponseDTO> carDriverInfo(@Validated @RequestBody DriverCarInfoRequestDTO requestDTO) {
        logger.info("carDriverInfo start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<DriverCarInfoResponseDTO> responseData = new ResponseData<>();
        try {
            DriverCarInfoResponseDTO responseDTO = driverBaseInfo.carDriverInfo(requestDTO.getCarId());
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            responseData.setCode(Code.Fail);
            responseData.setMsg("请求失败！");
        }
        logger.info("carDriverInfo end result -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 通过carId获取司机和车辆相关信息
     * @Date 2022/3/21 17:04
     **/
    @PostMapping("/carDriverInfoList")
    public ResponseData<List<DriverCarInfoResponseDTO>> carDriverInfoList(@Validated @RequestBody DriverCarInfoRequestDTO requestDTO) {
        logger.info("carDriverInfoList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<DriverCarInfoResponseDTO>> responseData = new ResponseData<>();
        try {
            List<DriverCarInfoResponseDTO> responseDTO = driverBaseInfo.carDriverInfoList(requestDTO.getCarIds());
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            responseData.setCode(Code.Fail);
            responseData.setMsg("请求失败！");
        }
        logger.info("carDriverInfoList end result -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

}
